<template>
  <div class="retry_page">
    <!-- 头部 -->
    <div class="ai_header">
      <img src="@/assets/images/ic_back@2x.png" @click="backRouter" />
      <span class="ai_title">{{ title }}</span>
    </div>
    <!-- 失败或超时，重试页面 -->
    <div class="fail_retry_page">
      <div class="defual_img_wrap">
        <img :src="failCoverImg" alt="" class="top_img" />
      </div>
      <div class="sorry_text">Sorry！</div>
      <div class="error_des_tip">A small error has occurred. Please try again</div>
      <div class="try_wrap" @click="retryEvent">
        <img src="@/assets/images/ic_retry@2x.png" />
        <span class="btn_name">Retry</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { showToast } from "vant";
import api from "@/api";
import router from "@/router";
import { useRoute } from "vue-router";
import { usePollingStore } from "@/stores/polling";

const route = useRoute();
const store = usePollingStore();

const failCoverImg = ref("");
const title = ref("AI Pose");

const backRouter = () => {
  router.back();
};

// 重试
const retryEvent = async () => {
  try {
    const response = await api.taskRetry({ taskNo: route.query.taskNo });
    if (response.code === 200) {
      router.back();
    } else {
      showToast("Retry failed");
    }
  } catch (err) {
    console.log(err);
  }
};

onMounted(() => {
  // 进入重试页面，恢复状态管理中的数据isPollFail
  store.isPollFail = false;
  // 判断来源
  if (route.query.from === "pose") {
    failCoverImg.value = sessionStorage.getItem("aiposeCover");
    title.value = "AI Pose";
  } else {
    failCoverImg.value = sessionStorage.getItem("removeAiposeCover");
    title.value = "REMOVE";
  }
});
</script>

<style scoped lang="less">
.retry_page {
  min-height: 100vh;
  box-sizing: border-box;
  padding: 0 16px;
  /* 背景图设置 */
  background-image: url("../assets/images/bg_texture@2x.png");
  background-position: top center;
  /* 从顶部开始 */
  background-repeat: no-repeat;
  /* 不重复 */
  background-size: 100% auto;
  /* 宽度满屏，高度自适应 */
  background-color: #fbfbfd;

  .ai_header {
    height: 56px;
    display: flex;
    position: relative;
    line-height: 56px;

    img {
      width: 24px;
      position: absolute;
      left: 0px;
      top: 16px;
    }

    .ai_title {
      font-size: 18px;
      font-weight: 700;
      margin-left: 46px;
    }
  }

  .fail_retry_page {
    .defual_img_wrap {
      width: 100%;
      height: 300px;
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        max-width: 100%;
        /* 限制最大宽度为容器宽度 */
        max-height: 100%;
        /* 限制最大高度为容器高度 */
        width: auto;
        /* 保持原始宽度比例 */
        height: auto;
        /* 保持原始高度比例 */
        object-fit: contain;
        /* 保持比例，完整显示图片 */
      }
    }

    .sorry_text {
      font-size: 24px;
      color: #ed0067;
      margin-top: 16px;
      text-align: center;
      font-weight: 700;
    }

    .error_des_tip {
      width: 225px;
      font-family: Inter, Inter;
      font-weight: 400;
      font-size: 16px;
      color: #000000;
      margin: 0 auto;
      margin-top: 8px;
      text-align: center;
    }

    .try_wrap {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 148px;
      height: 64px;
      border-radius: 8px 8px 8px 8px;
      border: 1px solid rgba(0, 0, 0, 0.45);
      margin: 0 auto;
      margin-top: 20px;

      img {
        width: 32px;
        margin-right: 10px;
      }

      .btn_name {
        font-size: 18px;
        color: #000000;
      }
    }
  }
}
</style>
